import r from "../packages/57812"
import viewmodeEnum from "../enum/viewmode.enum"

import OrthographicControlsModule from "./OrthographicControlsModule"
import SymbolList from "../SymbolList"
import { RotateEndEvent, RotateEvent } from "../event/GestureEvent"
import hotKeyEnum, { KeyState } from "../enum/hotKey.enum"
import OrthographicCameraControls from "../controls/OrthographicCameraControls"
import MeshData from "../data/MeshData"
const g = Math.PI / 2 / 1000
var f = {
  0: "NONE",
  1: "PAN",
  2: "ZOOM",
  4: "ROTATE",
  NONE: 0,
  PAN: 1,
  ROTATE: 4,
  ZOOM: 2
}
export default class FloorplanControlsModule extends OrthographicControlsModule {
  declare controls: OrthographicCameraControls
  constructor() {
    super()
    this.name = "floorplan-controls"
  }
  async init(e, t) {
    await super.init(e, t)
    t.getModuleBySymbol(SymbolList.INPUT).then(e => {
      this.bindings.push(
        e.registerHandler(RotateEvent, e => {
          this.validateViewmode() && (this.controls.setRollAcceleration(e.rotateDelta), this.controls.update(r.SI), this.controls.stop())
        })
      ),
        this.bindings.push(e.registerHandler(RotateEndEvent, this.resetControlState))
    })
  }

  createCameraControls(e: MeshData) {
    this.controls = new OrthographicCameraControls(this.cameraModule, e.bounds, e.center, !0)
    this.commonControlsModule.addControls(viewmodeEnum.Floorplan, this.controls)
  }
  onDrag(e) {
    super.onDrag(e)
    switch (this.controlState) {
      case f.ROTATE:
        this.controls.setRollAcceleration(e.x * Math.PI)
    }
  }
  onKey(e) {
    super.onKey(e)
    const { key: t, state: i, modifiers: n } = e,
      s = i === KeyState.DOWN
    switch (t) {
      case hotKeyEnum.J:
      case hotKeyEnum.LEFTARROW:
        this.controls.setRollAcceleration(s ? g : 0, !0)
        break
      case hotKeyEnum.L:
      case hotKeyEnum.RIGHTARROW:
        this.controls.setRollAcceleration(s ? -g : 0, !0)
        break
      case hotKeyEnum.DOWNARROW:
        n.shiftKey || this.controls.setZoomAcceleration(s ? r.Gu : 0)
        break
      case hotKeyEnum.UPARROW:
        n.shiftKey || this.controls.setZoomAcceleration(s ? -r.Gu : 0)
    }
  }
  validateViewmode() {
    return this.viewmodeData.currentMode === viewmodeEnum.Floorplan
  }
}
